#!/usr/bin/env bash
SCRIPT_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DOTFILES_ROOT_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
R_ROOT_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && cd ../../.. && pwd )"

cd ~
pwd
set -x
set -e


err_exit() {
    exit 1;
}


create_symlink () {
    cd ~
    rm -f $1
    ln -s $2 $1
}
create_dirsymlink () {
    cd ~
    if [[ x$1 == x/* ]] ; then
        echo bad usage;
        return
    fi
    if [ -d $2 ]; then
        rm -f $1 || err_exit "you need first remove dir:$1"
        ln -s $2 $1
    else
        echo not target dir
    fi
}

cd ~

create_dirsymlink dotfiles ${DOTFILES_ROOT_DIR}
create_dirsymlink .vim dotfiles/.vim
create_symlink .bash_prompt dotfiles/.bash_prompt
create_symlink .curlrc dotfiles/.curlrc
create_symlink .editorconfig dotfiles/.editorconfig
create_symlink .exports dotfiles/.exports
create_symlink .functions dotfiles/.functions
create_symlink .gdbinit dotfiles/.gdbinit
create_symlink .hgignore dotfiles/.hgignore
create_symlink .hushlogin dotfiles/.hushlogin


create_dirsymlink .zsh dotfiles/00my/.zsh
create_symlink .zshrc dotfiles/00my/.zshrc
create_symlink .inputrc dotfiles/00my/.inputrc
create_symlink .npmrc dotfiles/00my/.npmrc
create_symlink .pythonrc dotfiles/00my/.pythonrc
create_symlink .lldbinit dotfiles/00my/.lldbinit
create_symlink .bashrc dotfiles/00my/.bashrc
create_symlink .profile dotfiles/00my/.profile
create_symlink .zprofile dotfiles/00my/.zprofile
create_symlink .gitconfig dotfiles/00my/.gitconfig
create_symlink .gitignore dotfiles/00my/.gitignore
create_symlink .gitattributes dotfiles/00my/.gitattributes
create_symlink .vimrc dotfiles/00my/.vimrc
create_symlink .gvimrc dotfiles/00my/.gvimrc



create_dirsymlink .ssh ${R_ROOT_DIR}/my/dot.ssh/
#https://medium.com/@tahteche/how-git-treats-changes-in-file-permissions-f71874ca239d
# file permission user+group+other, because git not record group+other permission, need chmod 600 .ssh/config
chmod 600 ~/.ssh/*

cd ${SCRIPT_DIR} || err_exit 

which gita 2>&1 /dev/null || pip3 install -U gita

../git-sync-deps
(cd ../.vim/pack/myqpack/start/python-mode/ && {
   #git submodule update --init --recursive
})
apt install python2.7-dev || true
(cd ../.vim/pack/myqpack/start/YouCompleteMe/ && {
   #git submodule update --init --recursive
   #./install.py
})


if [ "x`uname`" == "xDarwin" ] && [ -f install_mac.sh ] ; then
  ./install_mac.sh
fi

if [ "x`uname`" == "xubuntu" ] && [ -f install_ubuntu.sh ] ; then
  ./install_ubuntu.sh
fi





